const e = require("express");
const db = require("../config/db");


class Admin {
    // 注册账号
    register(admin_name, admin_email, admin_phone, admin_pwd, admin_createTime) {
        return new Promise((resolve, reject) => {
            db.query(`select admin_email from db_lqyAdmin where admin_email = '${admin_email}'`)
                .then(
                    rows => {
                        if (rows && rows.length === 0) {
                            // 不存在就添加账号
                            // 查询db_message这个表格的所有字段
                            let sql = `insert into db_lqyAdmin(admin_name, admin_email, admin_phone, admin_pwd, admin_createTime) values('${admin_name}','${admin_email}','${admin_phone}','${admin_pwd}','${admin_createTime}')`;
                            db.query(sql)
                                .then(
                                    content => {
                                        resolve({
                                            code: 200,
                                            msg: "注册成功！",
                                            content
                                        })
                                    }
                                )
                                .catch(
                                    err => {
                                        reject({
                                            code: 500,
                                            msg: "注册失败！",
                                            err,
                                        })
                                    }
                                )
                        } else {
                            resolve({
                                code: 1,
                                msg: "该账号已存在！",
                                admin_email,
                            })
                        }
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: -1,
                            msg: "查询账号信息失败！",
                            err,
                        })
                    }
                )
        })
    }


    // 登录账号
    login(admin_email) {
        return new Promise((resolve, reject) => {
            db.query(`SELECT * FROM db_lqyAdmin WHERE admin_email = '${admin_email}'`)
                .then(
                    rows => {
                        // if (rows.length > 0) {
                        //     resolve(rows[0]); // 返回第一个匹配的用户数据
                        // } else {
                        //     resolve(null); // 如果未找到用户，则解析为 null
                        // }
                        if (rows && rows.length === 1) {
                            resolve(rows[0]);
                        } else {
                            resolve({
                                code: 404,
                                msg: "该账号不存在！",
                                rows
                            })
                        }
                    })
                .catch(
                    err => {
                        // reject(err); // 如果查询出错，拒绝 Promise 并传递错误
                        reject({
                            code: 404,
                            msg: "服务器异常！",
                            err,
                        })
                    });
        });
    }


    // 获取登录用户的账号信息
    info(id) {
        return new Promise((resolve, reject) => {
            db.query(`select admin_name, admin_email, admin_phone, admin_createTime from db_lqyAdmin where id = '${id}'`)
                .then(
                    rows => {
                        if (rows.length === 1) {
                            resolve({
                                code:200,
                                msg: "获取登录用户信息成功！",
                                rows
                            })
                        } else {
                            resolve({
                                code: 200,
                                msg: "该账号不存在！",
                                rows,
                            })
                        }
                    }
                )
                .catch(
                    err => {
                        reject({
                            code: 200,
                            msg: "服务器异常！",
                            err,
                        })
                    }
                )
        })
    }

    // 注册账号
    loooo(admin_email, admin_pwd) {
        return new Promise((resolve, reject) => {
            db.query(`select admin_pwd from db_lqyAdmin where admin_email = '${admin_email}'`)
                .then()
                .catch(
                    err => {
                        reject({
                            code: 200,
                            msg: "",
                        })
                    }
                )
        })
    }

    // 注册账号
    loooo(admin_email, admin_pwd) {
        return new Promise((resolve, reject) => {
            db.query(`select admin_pwd from db_lqyAdmin where admin_email = '${admin_email}'`)
                .then()
                .catch(
                    err => {
                        reject({
                            code: 200,
                            msg: "",
                        })
                    }
                )
        })
    }

}



module.exports = new Admin();